package com.proton.carepatchtemp.base;


import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.apkfuns.logutils.LogUtils;
import com.proton.carepatchtemp.R;
import com.proton.carepatchtemp.application.VCare;
import com.proton.carepatchtemp.utils.eventbus.MainBusListener;
import com.proton.carepatchtemp.utils.eventbus.MainEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MyDialogFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MyDialogFragment extends DialogFragment implements MainBusListener {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private int requestCode;
    private String title;
    private String msg;
    private String cancleText = VCare.get().getString(R.string.string_cancel);
    private String okText = VCare.get().getString(R.string.string_confirm);
    private int titleImage = R.mipmap.ic_launcher;
    private ImageView mIvTitle;
    private int result = 0;
    /**
     * 付款提醒
     */
    private TextView mTvTitle;
    /**
     * 普通咨询可以通过信件方式咨\n询医生一次,需要支付10元。
     */
    private TextView mTvMsg;
    /**
     * 取消
     */
    private TextView mTvCancel;
    /**
     * 继续
     */
    private TextView mTvContinue;
    private OnDialogListener mlistener;

    public MyDialogFragment() {
    }

    public static MyDialogFragment newInstance(int requestCode, String param2) {
        MyDialogFragment fragment = new MyDialogFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PARAM1, requestCode);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            requestCode = getArguments().getInt(ARG_PARAM1);
        }
        initBus();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        View view = inflater.inflate(R.layout.fragment_my_dialog, container, false);
        // Inflate the layout for this fragment
        initView(view);
        inData();

        setListener();
        return view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unRegisterEventBus();
    }

    public void dealEvent(MainEvent event) {

    }

    private void inData() {
        mIvTitle.setImageResource(titleImage);
        mTvTitle.setText(title);
        mTvMsg.setText(msg);
        mTvCancel.setText(cancleText);
        mTvContinue.setText(okText);
    }

    private void initView(View view) {
        mIvTitle = view.findViewById(R.id.iv_title);
        mTvTitle = view.findViewById(R.id.tv_title);
        mTvMsg = view.findViewById(R.id.tv_msg);
        mTvCancel = view.findViewById(R.id.tv_cancel);
        mTvContinue = view.findViewById(R.id.tv_continue);
    }

    private void setListener() {
        mTvCancel.setOnClickListener(v -> {
            result = 1;
            if (null != mlistener) {
                mlistener = null;
            }
            MyDialogFragment.this.dismiss();
        });

        mTvContinue.setOnClickListener(v -> {
            result = 2;
            if (null != mlistener) {
                mlistener.onDialogClick(requestCode, result);
                mlistener = null;
            }
            MyDialogFragment.this.dismiss();
        });
    }

    public void setCancelText(String cancelText) {
        this.cancleText = cancelText;
    }

    public void setOkText(String okText) {
        this.okText = okText;
    }

    public void setTitle(String title, @DrawableRes int res) {
        this.title = title;
        mIvTitle.setImageResource(res);
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void initBus() {
       /* if(new EventBus().isRegistered(this)){
            EventBus.getDefault().register(BaseFragment.this);
        }*/
        EventBus.getDefault().register(MyDialogFragment.this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void dealOnEvent(MainEvent event) {
        if (canDeal(event)) {
            try {
                dealEvent(event);
            } catch (Throwable e) {
                LogUtils.e(e);
            }
        }
    }

    public boolean canDeal(MainEvent event) {
        return event != null && getActivity() != null && !getActivity().isFinishing();
    }

    public void unRegisterEventBus() {
        EventBus.getDefault().unregister(MyDialogFragment.this);
    }

    public void setOnDialogListener(OnDialogListener dialogListener) {
        this.mlistener = dialogListener;
    }

    @Override
    public void onEventMainThread(MainEvent event) {
        dealOnEvent(event);
    }

    public interface OnDialogListener {
        void onDialogClick(int requestCode, int result);
    }
}
